package com.supermap.dataservice.cloud.cust.role;

import com.supermap.dubbo.api.cloud.cust.menu.TreeNode;
import com.supermap.dubbo.api.cloud.cust.role.TreeNodeList;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @desc
 * @date 2019年02月28 16:23
 * @email 1301584033@qq.com
 */
@Mapper
public interface SysAreaRoleMapper {

    //----------------------------------------------------区域角色菜单查询---------------

    @SelectProvider(type =SysAreaRoleMapperSQL.class,method = "getAllAreaByAreaCodeSQL")
    List<TreeNodeList> getAllAreaByAreaCode(@Param("areaCode")String  areaCode);

    @SelectProvider(type =SysAreaRoleMapperSQL.class,method = "getAllAreaRoleByRoleIdSQL")
    List<TreeNodeList> getAllAreaRoleByRoleId(@Param("areaCode")String  areaCode,@Param("roleId")Integer  roleId);
    //区域菜单查询
    @SelectProvider(type =SysAreaRoleMapperSQL.class,method = "getTreeAreaRoleMenuSQL")
    List<TreeNodeList> getTreeAreaRoleMenu(@Param("areaCode")String  areaCode,@Param("roleId")Integer  roleId,@Param("length")Integer length,@Param("code")String code);


    @InsertProvider(type = SysAreaRoleMapperSQL.class,method = "putRoleAreaMenuSQL")
    Integer putRoleAreaMenu(@Param("menuId")String menuId,@Param("roleId")Integer roleId);
    //----------------------------------------------------------区域分配角色------------

    @Select("select t1.areacode as id,t1.areaname as name from t_area t1  where length(t1.areacode)=2 ")
    List<TreeNode> getAllArea();

    @Select("select t1.id,t1.name, (select  count(id) from sys_area_role where areacode=#{areaCode} and roleid =t1.id) as category  from t_role  t1")
    List<TreeNode> getAllRole(@Param("areaCode")String  areaCode);

    @Insert("insert into sys_area_role(areacode,roleid) values(#{areaCode},${roleId})")
    Integer putAreaRole(@Param("areaCode")String areaCode,@Param("roleId")String roleId);

    @Delete("delete from sys_area_role where areacode=#{areaCode}")
    Integer delAreaRole(@Param("areaCode")String areaCode);

    class  SysAreaRoleMapperSQL{
        public String  putRoleAreaMenuSQL(Map<String,Object> map){
            StringBuilder stringBuilder=new StringBuilder();
            String menuIds=(String)map.get("menuId");
            Integer roleId=(Integer)map.get("roleId");
                stringBuilder.append("insert into sys_area_role_menu(menuid,arearoleid) values ");
            for (String str:menuIds.split(",")){
                stringBuilder.append("("+Integer.valueOf(str)+","+roleId+"),");
            }

            return stringBuilder.toString().substring(0,stringBuilder.length()-1);
        }
        public String getTreeAreaRoleMenuSQL(Map<String,Object> map){
            String code=(String)map.get("code");
            String areaCode=(String)map.get("areaCode");
            Integer roleId=(Integer)map.get("roleId");
            Integer length=(Integer)map.get("length");
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append(" select t1.id,t1.name,t1.code,t1.url,t1.category,t1.functiontype,t1.icon,t1.orderNum,date_format(t1.createtime,'%Y-%m-%d %H:%i:%S') as createtime,t1.createuser,date_format(t1.updatetime,'%Y-%m-%d %H:%i:%S') as updatetime,t1.updateuser,(select count(id) from sys_area_role_menu where arearoleid="+roleId+" and menuid=t1.id) as state from sys_menu t1 " +
                    "  inner join sys_area_menu t2  " +
                    "   on t1.id=t2.menuid where  1=1 and t1.state=0 ");
            if(areaCode!=null){
                stringBuilder.append(" and t2.areacode like concat(#{areaCode},'%') ");
            }

            if(code!=null){
                stringBuilder.append(" and t1.code like concat('"+code+"','%') ");
            }
            if(length!=null){
                stringBuilder.append(" and length(t1.code)="+length+"  ");
            }
            return stringBuilder.toString();
        }

        /**
         * 查询
         * */
        public String getAllAreaByAreaCodeSQL(Map<String,Object> map){
            String areaCode=(String)map.get("areaCode");
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append("select t1.areacode as id,t1.areaname as name,t1.areacode as code from t_area t1  where length(t1.areacode)=2  ");
            if(areaCode!=null){
                stringBuilder.append("  and t1.areacode="+areaCode+"");
            }
            return stringBuilder.toString();
        }
        public String getAllAreaRoleByRoleIdSQL(Map<String,Object> map){
            String areaCode=(String)map.get("areaCode");
            Integer roleId=(Integer)map.get("roleId");
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append("select t2.id,t1.name from t_role t1\n" +
                    "inner join sys_area_role t2\n" +
                    "on t1.id=t2.roleid\n" +
                    "inner join sys_area_role_menu t3\n" +
                    "on t2.id=t3.arearoleid  where 1=1 and t1.del_flg=0  ");
            if(areaCode!=null){
                stringBuilder.append("  and  t2.areacode= "+areaCode+" ");
            }
            if(roleId!=null){
                stringBuilder.append("  and  t2.id= "+roleId+" ");
            }
            return stringBuilder.toString();
        }

    }


}
